<?php
class Calendar
{
    private $year;
    private $month;
    private $weeks  = array('日','一','二','三','四','五','六');
    private $data;
    function display($year='' , $month="",$data = '')    {
    	if (empty($year)){
    		$this->year =  date('Y');
    	}
    	else{
    		$this->year = $year;
    	}
    	if (empty($month)){
    		$this->month = date('m');
    	}
    	else{
    		$this->month = $month;
    	}
    	if (!empty($data)){
    		$this->data=$data;
    	}
    	
        $tmp =  '<ul class="dataTable">'. $this->showWeeks().$this->showDays($this->year,$this->month).'</ul>';
      //  $this->showChangeDate();
      
        return $tmp;
        
    }
    function display1($year='' , $month='',$data = '')    {
    	if (empty($year)){
    		$this->year =  date('Y');
    	}
    	else{
    		$this->year = $year;
    	}
    	if (empty($month)){
    		$this->month = date('m');
    	}
    	else{
    		$this->month = $month;
    	}
    	if (!empty($data)){
    		$this->data=$data;
    	}
    	 
    	$tmp =  '<ul class="dataTable">'. $this->showWeeks().$this->showDays1($this->year,$this->month).'</ul>';
    	//  $this->showChangeDate();
    
    	return $tmp;
    
    }
    private function showWeeks()
    {
		$tmp= '' ; 
        foreach($this->weeks as $title)
        {
             $tmp = $tmp.'<li class="Dx1">'.$title.'</li>';
        }
 		return $tmp;
    }
    
    private function showDays($year, $month)
    {
        $firstDay = mktime(0, 0, 0, $month, 1, $year);
        $starDay = date('w', $firstDay);
        $days = date('t', $firstDay);
        $tmp4 = '';
        for ($i=0; $i<$starDay; $i++) {
            $tmp4 = $tmp4. '<li></li>';
        }
        $tmp  =  strtotime(date('Y-m-d'));
        
        for ($j=1; $j<=$days; $j++) {
            $i++;
            $tmp2 = strtotime((date('Y-m',$firstDay).'-'.$j));
            $tmp1 =  $tmp2 - $tmp;
			if ($tmp1>=0 && $tmp1<1296000){
				$tmp3 = '';
				if ($tmp2 ==$this->data){
					$tmp3 = 'class="AlvSe"'  ;
				}
				
				$pos = stripos($_SERVER['REQUEST_URI'],'?');
				$request_uri = $_SERVER['REQUEST_URI'];
				if($pos>0){
					$request_uri = substr($request_uri,0,$pos);
				}
				
				$site_url = 'http://'.$_SERVER['HTTP_HOST'].$request_uri;
	    		if(!preg_match("/^[0-9]+$/",$tmp2)){
	    			$today_time = strtotime(date('Y-m-d'));
					$tmp4 = $tmp4. '<li><a href="'.$site_url.'?&date='.$today_time.'"  '.$tmp3.'   >'.$j.'</a></li>';
	    		}					
				$tmp4 = $tmp4. '<li><a href="'.$site_url.'?&date='.$tmp2.'"  '.$tmp3.'   >'.$j.'</a></li>';
			}
            else {
                $tmp4 = $tmp4.'<li>'.$j.'</li>';
            }
        }
        return $tmp4 ;
    }
    
    private function showDays1($year, $month)
    {
    	$firstDay = mktime(0, 0, 0, $month, 1, $year);
    	$starDay = date('w', $firstDay);
    	$days = date('t', $firstDay);
    	$tmp4 = '';
    	for ($i=0; $i<$starDay; $i++) {
    		$tmp4 = $tmp4. '<li></li>';
    	}
    	$tmp  =  strtotime(date('Y-m-d'));
    	for ($j=1; $j<=$days; $j++) {
    		$i++;
    		$tmp2 = strtotime((date('Y-m',$firstDay).'-'.$j));
    		$tmp1 =  $tmp2 - $tmp;
    		$tmp3 = '';
    		if ($tmp2 ==$this->data){
    			$tmp3 = 'class="AlvSe"'  ;
    		}

    		$pos = stripos($_SERVER['REQUEST_URI'],'?');
    		$request_uri = $_SERVER['REQUEST_URI'];
    		if($pos>0){
    			$request_uri = substr($request_uri,0,$pos);
    		}
    		
    		$site_url = 'http://'.$_SERVER['HTTP_HOST'].$request_uri;    
    		if(!preg_match("/^[0-9]+$/",$tmp2)){
    			$today_time = strtotime(date('Y-m-d'));
	    		$tmp4 = $tmp4. '<li><a href="'.$site_url.'?&date="'.$today_time.'"'.$tmp3.'   >'.$j.'</a></li>';
    		}		
    		$tmp4 = $tmp4. '<li><a href="'.$site_url.'?&date='.$tmp2.'"  '.$tmp3.'   >'.$j.'</a></li>';
    	}
    	return $tmp4 ;
    }
    private function showChangeDate()
    {
        $url = basename($_SERVER['PHP_SELF']);
        echo '<tr>';
	echo '<td><a href="?'.$this->preYearUrl($this->year,$this->month).'">'.'<<'.'</a></td>';
	echo '<td><a href="?'.$this->preMonthUrl($this->year,$this->month).'">'.'<'.'</a></td>';
        echo '<td colspan="3"><form>';
        
        echo '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
        for($ye=1970; $ye<=2038; $ye++) {
            $selected = ($ye == $this->year) ? 'selected' : '';
            echo '<option '.$selected.' value="'.$ye.'">'.$ye.'</option>';
        }
        echo '</select>';
        echo '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value+\'\'">';
        

        
        for($mo=1; $mo<=12; $mo++) {
            $selected = ($mo == $this->month) ? 'selected' : '';
            echo '<option '.$selected.' value="'.$mo.'">'.$mo.'</option>';
        }
        echo '</select>';        
        echo '</form></td>';        
	echo '<td><a href="?'.$this->nextMonthUrl($this->year,$this->month).'">'.'>'.'</a></td>';
	echo '<td><a href="?'.$this->nextYearUrl($this->year,$this->month).'">'.'>>'.'</a></td>';        
        echo '</tr>';
    }
    
    private function preYearUrl($year,$month)
    {
        $year = ($this->year <= 1970) ? 1970 : $year - 1 ;
        
        return 'year='.$year.'&month='.$month;
    }
    
    private function nextYearUrl($year,$month)
    {
        $year = ($year >= 2038)? 2038 : $year + 1;
        
        return 'year='.$year.'&month='.$month;
    }
    
    private function preMonthUrl($year,$month)
    {
        if ($month == 1) {
            $month = 12;
            $year = ($year <= 1970) ? 1970 : $year - 1 ;
        } else {
            $month--;
        }        
        
        return 'year='.$year.'&month='.$month;
    }
    
    private function nextMonthUrl($year,$month)
    {
        if ($month == 12) {
            $month = 1;
            $year = ($year >= 2038) ? 2038 : $year + 1;
        }else{
            $month++;
        }
        return 'year='.$year.'&month='.$month;
    }
    
}